// 结构体

#[derive(Debug)]
struct Rectangle {
    width:i32,
    height:i32,
    color:String
}

 // 结构实现
impl Rectangle {
     // 结构方法
    fn area(width:i32,height:i32)->i32 {
        width*height
    }

     // 构造函数
    pub fn new(width:i32,height:i32,color:String)->Self{Rectangle{width,height, color }}

    pub fn get_color(&self)->String {
        self.color.to_string()
    }
    pub fn set_color(&mut self,n_color:&str) {
        self.color = n_color.to_string();
    }

    pub fn drup_self(self) { // self随函数执行完成而销毁
        println!("drup_self")
    }
}


// 调用
pub fn main() {
    let area = Rectangle::area(2,4);
    println!("{}",area.to_string());

    let mut demo = Rectangle::new(6, 8,"red".to_string());
    println!("{:#?}",demo);
    println!("{:#?}",demo.height);
    println!("{:#?}",demo.width);

    
    println!("{:#?}",demo.get_color());
    demo.set_color("green");
    println!("{:#?}",demo.get_color());

    demo.drup_self();
    // println!("{:#?}",demo); // 无法通过编译检查，因为实例被销毁

}